<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/6
 * Time: 9:12
 */

namespace Admin\Controller;


class MessageController extends AdminController
{
     public function index(){
         $contact=M("contact");
         $where=array(
             'id'=>array('gt',0),
             'cate_id'=>array('gt',0),
         );
         $p=getpage($contact,$where,10);
         $message_data=$contact->field(true)->where($where)->order('id desc')->select();
         $this->page=$p->show();
         foreach ($message_data as $key => $val){
             $message_data[$key]['community'] = M("document")->where("`id`=".$val['community_id'])->getField("title");
             $message_data[$key]['cate_name'] = M("category")->where("`id`=".$val['cate_id'])->getField("title");
         }
         $this->assign("message_data",$message_data);
         $this->assign("meta_title","留言板");
         $this->display();
     }
    public function message(){
        $contact=M("contact");
        $where=array(
            'id'=>array('gt',0),
            'cate_id'=>array('eq',0),
        );
        $p=getpage($contact,$where,10);
        $message_data=$contact->field(true)->where($where)->order('id desc')->select();
        $this->page=$p->show();
        foreach ($message_data as $key => $val){
            $message_data[$key]['community'] = M("document")->where("`id`=".$val['community_id'])->getField("title");
        }
        $this->assign("message_data",$message_data);
        $this->assign("meta_title","留言板");
        $this->display();
    }

    public function edit($id = 0){
        $id=I("get.id",0,"int");
        $message=M("contact");
        $data=$message->where("`id`=".$id)->find();
        $data['community_name'] =M("document")->where("`id`=".$data['community_id'])->getField("title");
        $data['cate_name'] =M("category")->where("`id`=".$data['cate_id'])->getField("title");
        $this->assign("data",$data);
        $this->assign("meta_title","留言板");
        $this->display();
    }
    public function xq($id = 0){
        $id=I("get.id",0,"int");
        $message=M("contact");
        $data=$message->where("`id`=".$id)->find();
        $data['community_name'] =M("document")->where("`id`=".$data['community_id'])->getField("title");
        $data['imgs'] =json_decode($data['imgs'],true);
        $this->assign("data",$data);
        $this->assign("meta_title","留言板");
        $this->display();
    }
    public function del($ids = 0){
        if(empty($ids)){
            $this->error('参数错误！');
        }
        if(is_array($ids)){
            $map['id'] = array('in', $ids);
        }elseif (is_numeric($ids)){
            $map['id'] = $ids;
        }
        if(M('contact')->where($map)->delete()){
            //记录行为
            
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }
}